package edu.cdnu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.cdnu.common.dto.CatalogueDto;
import edu.cdnu.dao.FileDao;
import edu.cdnu.pojo.ResourcePojo;
import edu.cdnu.service.FileService;

@Service
public class FileServiceImpl implements FileService{
	
	@Autowired
	private FileDao fileDao;

	@Override
	public List<CatalogueDto> selectCatalogueDtoByParentId(Integer parentId) {
		if(parentId==null) {
			parentId=0;
		}
		return fileDao.selectCatalogueDtoByParentId(parentId);
	}

	
	@Override
	public List<CatalogueDto> selectCatalogueDtoHaveChildren() {
		// 根目录
		List<CatalogueDto> list=selectCatalogueDtoByParentId(0); 
		for(CatalogueDto c:list) {
			selectChildren(c);
		}
		return list;
	}

	
	/**
	 * 递归查询子目录
	 * @param catalogueDto
	 */
	public void selectChildren(CatalogueDto catalogueDto){
		// 通过id查子目录
		List<CatalogueDto> list=fileDao.selectCatalogueDtoByParentId(catalogueDto.getId());
		// 有，设置进去 并且查询下一级
		if(list.size()>0) {
			catalogueDto.setChildren(list);
			for(CatalogueDto c:list) {
				selectChildren(c);
			}
		}
		// 没有就不做操作
	}


	@Override
	public List<ResourcePojo> selectResourcePojoListByCatalogueId(int catalogueId) {
		return fileDao.selectResourcePojoListByCatalogueId(catalogueId);
	}


	@Override
	public void insertResource(String resourceName, String resourceUrl, int catalogueId) {
		fileDao.insertResource(resourceName, resourceUrl, catalogueId);
	}


	@Override
	public int deleteResource(int resourceId) {
		return fileDao.deleteResource(resourceId);
	}


	@Override
	public void insertCatalogue(String catalogueText, int parentId) {
		fileDao.insertCatalogue(catalogueText, parentId);
	}


	@Override
	public int deleteCatalogue(int catalogueId) {
		return fileDao.deleteCatalogue(catalogueId);
	}
	
}
